package com.lxyk.controller.user;

import com.lxyk.domain.Classes;
import com.lxyk.domain.PageBean;
import com.lxyk.domain.SysUser;
import com.lxyk.service.user.ClassesService;
import com.lxyk.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    @Autowired
    private UserService userService;

    /**--------------管理员-----------------*/

    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/findAll")
    public String findAll(Model model,Integer page,Integer size){
        if(page==null || page<0){
            page=0;
        }
        if(size==null || size<0){
            size=10;
        }
        PageBean pageBean = classesService.findAll(page, size);
        model.addAttribute("pageInfo", pageBean);
        return "classes-list";
    }

    //查询所有用户以及班级对应的用户
    @RequestMapping("/toAddTeacherPage")
    @Secured({"ROLE_ADMIN"})
    public String toAddTeacherPage(Model model,Integer id){
        Map<String, Object> map = classesService.toAddTeacherPage(id);
        model.addAttribute("cid", id);
        model.addAttribute("allUsers", map.get("allUsers"));
        model.addAttribute("myUsers", map.get("myUsers"));
        return "user-classes-add";
    }

    //修改班级的用户(先清空, 再往用户角色表中插入数据)
    // ids:选中的用户id   cid:班级id
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/addUserToClasses")
    public String addUserToClasses(Integer[] ids, Integer cid){
        classesService.addUserToClasses(ids,cid);
        return "redirect:findAll";
    }

    //根据id查询
    @RequestMapping("/findById")
    public String findById(Model model,Integer id){
        Classes classes = classesService.findById(id);
        model.addAttribute("classes",classes);
        return "classes-show";
    }

    //更新班级信息
    @RequestMapping("/update")
    public String update(HttpServletRequest request, Classes classes){
        boolean bool = classesService.update(classes);
        if(bool){
            request.getSession().setAttribute("msg", "修改成功!");
        }else{
            request.getSession().setAttribute("msg", "修改失败!");
        }
        return "redirect:findAll";
    }

    //删除班级
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/delete")
    public String delete(HttpServletRequest request, Integer id){
        boolean bool = classesService.delete(id);
        if(bool){
            request.getSession().setAttribute("msg", "删除成功!");
        }else{
            request.getSession().setAttribute("msg", "删除失败!");
        }
        return "redirect:findAll";
    }

    //添加班级
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/save")
    public String save(Classes classes) throws ParseException {
        boolean bool = classesService.save(classes);
        return "redirect:findAll";
    }

    /**--------------普通用户------------------*/
    //根据登录用户查询对应班级
    @RequestMapping("/findByUser")
    public String findByUser(Model model){
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //根据用户id查询对应班级
        List<Classes> list = classesService.findByUser(user.getId());
        model.addAttribute("list",list);
        return "user-classes-list";
    }

    @RequestMapping("/useFindById")
    public String useFindById(Model model,Integer id){
        Classes classes = classesService.findById(id);
        model.addAttribute("classes",classes);
        return "user-classes-show";
    }

    //更新班级信息
    @RequestMapping("/userUpdate")
    public String userUpdate(HttpServletRequest request, Classes classes){
        boolean bool = classesService.update(classes);
        if(bool){
            request.getSession().setAttribute("msg", "修改成功!");
        }else{
            request.getSession().setAttribute("msg", "修改失败!");
        }
        return "redirect:findByUser";
    }



}
